package com.yjb.auth.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjb.auth.entity.RoleMenuRel;
import com.yjb.auth.entity.UserRoleRel;
import com.yjb.auth.mapper.RoleMenuRelMapper;
import com.yjb.auth.service.RoleMenuRelService;
import com.yjb.auth.service.UserRoleRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleMenuRelServiceImpl extends ServiceImpl<RoleMenuRelMapper, RoleMenuRel> implements RoleMenuRelService {

    @Autowired
    UserRoleRelService userRoleService;

    @Override
    public List<RoleMenuRel> listByRoleId(Integer roleId) {
        return list(Wrappers.<RoleMenuRel>query().lambda().eq(RoleMenuRel::getRoleId, roleId));
    }

    @Override
    public List<RoleMenuRel> listByRoleIds(List<Integer> roleIds) {
        return list(Wrappers.<RoleMenuRel>query().lambda().in(RoleMenuRel::getRoleId, roleIds));
    }

    @Override
    public List<RoleMenuRel> listByUserId(Integer userId) {
        List<UserRoleRel> userRoleList = userRoleService.listByUserId(userId);
        List<Integer> roleIdList = userRoleList.stream().map(UserRoleRel::getRoleId).distinct().collect(Collectors.toList());
        return listByRoleIds(roleIdList);
    }
}
